<template>
  <div class="app-container">
    <!-- 顶部搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="年度" prop="nd">
        <el-input size="mini"
                  v-model="queryParams.nd"
                  placeholder="请输入年度"
                  clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="xm">
        <el-input size="mini"
                  v-model="queryParams.xm"
                  placeholder="请输入姓名"
                  clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="专业方向" prop="zyFx">
        <el-select size="mini" v-model="queryParams.zyFx" placeholder="请选择专业方向" clearable>
          <el-option
            v-for="dict in dict.type.wbzjgl_zyfx"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="参与合作时间">
        <el-date-picker size="mini"
                        v-model="daterangeCyHzSj"
                        style="width: 240px"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 顶部按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['zjgl:wbzjgl:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['zjgl:wbzjgl:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['zjgl:wbzjgl:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['zjgl:wbzjgl:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['zjgl:wbzjgl:import']"
        >导入
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格 -->
    <el-table @row-click="clickRow" ref="table" highlight-current-row
              :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
              v-loading="loading" :data="wbzjglList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="序号" align="center" type="index" :index="indexMethod"/>
      <el-table-column label="年度" align="center" prop="nd"/>
      <el-table-column label="姓名" align="center" prop="xm"/>
      <el-table-column label="职务" align="center" prop="zw"/>
      <el-table-column label="职称" align="center" prop="zc"/>
      <el-table-column label="工作单位" align="center" prop="gzDw"/>
      <el-table-column label="联系电话" align="center" prop="lxDh"/>
      <el-table-column label="专业方向" align="center" prop="zyFx">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wbzjgl_zyfx" :value="scope.row.zyFx"/>
        </template>
      </el-table-column>
      <el-table-column label="参与合作时间" align="center" prop="cyHzSj" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cyHzSj, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合作评价" align="center" prop="hzPj">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wbzjgl_hzpj" :value="scope.row.hzPj"/>
        </template>
      </el-table-column>
      <el-table-column label="专家简介" align="center" prop="zjJj"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['zjgl:wbzjgl:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['zjgl:wbzjgl:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 导入对话框 -->
    <vxe-modal :title="upload.title" v-model="upload.open" width="400px" showFooter show-zoom resize>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                   @click="importTemplate">下载模板
          </el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </vxe-modal>

    <!-- 添加或修改外部专家管理对话框 -->
    <vxe-modal :title="title" v-model="open" width="60%" showFooter show-zoom resize>
      <el-form ref="formDialog" :model="form" :rules="rules" label-width="100px">
        <div style="height: 38vh;">
          <!-- 基本资料 -->
          <el-card class="box-card">
<!--            <div slot="header" class="clearfix" style="text-align: center;">-->
<!--              <span style="font-size: 20px; color: #1890ff">基本资料</span>-->
<!--            </div>-->
            <el-row :gutter="24">
              <el-col :span="12">
            <el-form-item label="年度" prop="nd">
              <el-input v-model="form.nd" placeholder="请输入年度"/>
            </el-form-item>
              </el-col>
              <el-col :span="12">
            <el-form-item label="姓名" prop="xm">
              <el-input v-model="form.xm" placeholder="请输入姓名"/>
            </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="24">
              <el-col :span="12">
            <el-form-item label="职务" prop="zw">
              <el-input v-model="form.zw" placeholder="请输入职务"/>
            </el-form-item>
              </el-col>
              <el-col :span="12">
            <el-form-item label="职称" prop="zc">
              <el-input v-model="form.zc" placeholder="请输入职称"/>
            </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="24">
              <el-col :span="12">
            <el-form-item label="工作单位" prop="gzDw">
              <el-input v-model="form.gzDw" placeholder="请输入工作单位"/>
            </el-form-item>
              </el-col>
              <el-col :span="12">
            <el-form-item label="联系电话" prop="lxDh">
              <el-input v-model="form.lxDh" placeholder="请输入联系电话"/>
            </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="24">
              <el-col :span="12">
            <el-form-item label="专业方向" prop="zyFx">
              <el-select v-model="form.zyFx" placeholder="请选择专业方向">
                <el-option
                  v-for="dict in dict.type.wbzjgl_zyfx"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
              </el-col>
              <el-col :span="12">
            <el-form-item label="参与合作时间" prop="cyHzSj">
              <el-date-picker clearable style="width: 100%;"
                              v-model="form.cyHzSj"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择参与合作时间">
              </el-date-picker>
            </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="24">
              <el-col :span="12">
            <el-form-item label="合作评价" prop="hzPj">
              <el-select v-model="form.hzPj" placeholder="请选择合作评价">
                <el-option
                  v-for="dict in dict.type.wbzjgl_hzpj"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
              </el-col>
            </el-row>
            <el-form-item label="专家简介" prop="zjJj">
              <el-input v-model="form.zjJj" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-card>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer dialog-footer-center">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </vxe-modal>
  </div>
</template>

<script>
import {addWbzjgl, delWbzjgl, getWbzjgl, getWbZjGlId, listWbzjgl, updateWbzjgl} from "@/api/zjgl/wbzjgl";
import {getToken} from "@/utils/auth";

export default {
  name: "Wbzjgl",
  dicts: ['wbzjgl_hzpj', 'wbzjgl_zyfx'],
  data() {
    return {
      getWbZjGlId: null, //从后端获取到的外部专家管理主键ID (UUID)
      selectedRow: null, // 保存当前选中的行
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 外部专家管理表格数据
      wbzjglList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 部门ID时间范围
      daterangeCyHzSj: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nd: null,
        xm: null,
        zyFx: null,
        cyHzSj: null,
        idWhere: null,
        userId: null,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        nd: [{required: true, message: "年度不能为空", trigger: "blur"}],
        xm: [{required: true, message: "姓名不能为空", trigger: "blur"}],
      },
      // 导入参数
      upload: {
        // 是否显示弹出层（导入）
        open: false,
        // 弹出层标题（导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/zjgl/wbzjgl/importData"
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //点击行 获取行
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      const selectedRows = table.selection;

      // 否则取消之前选中的行，并选中当前点击的行
      selectedRows.forEach(selectedRow => {
        table.toggleRowSelection(selectedRow);
      });
      table.toggleRowSelection(row);
    },
    /** 自定义序号 */
    indexMethod(index) {
      let pageNum = this.queryParams.pageNum - 1;
      if ((pageNum !== -1 && pageNum !== 0)) {
        return (index + 1) + (pageNum * this.queryParams.pageSize);
      } else {
        return (index + 1)
      }
    },
    /** 查询外部专家管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCyHzSj && '' != this.daterangeCyHzSj) {
        this.queryParams.params["beginCyHzSj"] = this.daterangeCyHzSj[0];
        this.queryParams.params["endCyHzSj"] = this.daterangeCyHzSj[1];
      }
      listWbzjgl(this.queryParams).then(response => {
        this.wbzjglList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        wbzjglId: this.getWbZjGlId,
        nd: new Date().getFullYear(),
        xm: null,
        zw: null,
        zc: null,
        gzDw: null,
        lxDh: null,
        zyFx: null,
        cyHzSj: null,
        hzPj: null,
        zjJj: null,
        idWhere: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        userId: null,
        deptId: null
      };
      this.resetForm("formDialog");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCyHzSj = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.wbzjglId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      getWbZjGlId().then(res => {
        this.getWbZjGlId = res.msg
        this.reset();
      })
      this.open = true;
      this.title = "添加外部专家管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const wbzjglId = row.wbzjglId || this.ids
      getWbzjgl(wbzjglId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改外部专家管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["formDialog"].validate(valid => {
        if (valid) {
          if (this.form.idWhere != null) {
            updateWbzjgl(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWbzjgl(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const wbzjglIds = row.wbzjglId || this.ids;
      this.$modal.confirm('确定删除选中记录？').then(function () {
        return delWbzjgl(wbzjglIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('zjgl/wbzjgl/export', {
        ...this.queryParams
      }, `wbzjgl_${new Date().getTime()}.xlsx`)
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('zjgl/wbzjgl/importTemplate', {}, `template_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "外部专家管理导入";
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", {dangerouslyUseHTMLString: true});
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
